<script setup>
import { computed, watch } from 'vue';
import useStore from '../store';
// 导入 store 对象
 const {todos} =useStore()
 // 定义计算属性完成全选功能
 const isAll=computed({
  set(value) {
    //接收状态，通过大选来控制小选状态
  // console.log(value);
  todos.checkAll(value)
  },
  get() {
    //计算属性必须要有返回值
    return todos.list.every(item=>item.done)
  }
 })
 //将数据持久化存储
   todos.$subscribe(()=>{
    localStorage.setItem('todos', JSON.stringify(todos.list))
   })
</script>
<template>
  <section class="main">
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in todos.list" :key="item.id">
        <div class="view">
          <!-- 定义change事件改变状态 -->
          <input @change="todos.changeState(item.id)" class="toggle" type="checkbox" :checked="item.done" />
          <label>{{ item.name }}</label>
          <button @click="todos.delTask(item.id)" class="destroy"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped></style>
